﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WaterApp.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUsers();
            }
        }

        private void BindUsers()
        {
            UserGrid.DataSource = Membership.GetAllUsers();
            UserGrid.DataBind();
        }

        protected void UserGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            UserGrid.CurrentPageIndex = e.NewPageIndex;
            BindUsers();
        }

        protected void lnkunlock_Click(object sender, EventArgs e)
        {
            try
            {
                if (UserGrid.SelectedItem != null)
                {
                    string uname = UserGrid.SelectedItem.Cells[3].Text.Trim();
                    if (Membership.GetUser(uname).IsLockedOut)
                    {
                        Membership.GetUser(uname).UnlockUser();
                        BindUsers();
                    }
                }
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected void lnkDeluser_Click(object sender, EventArgs e)
        {
            if (Membership.DeleteUser(UserGrid.SelectedItem.Cells[3].Text))
            {
                BindUsers();
            }
        }

        protected void lnkSaveUser_Click(object sender, EventArgs e)
        {
            MembershipUser mbUser 
                = Membership.CreateUser(UserName.Text.Trim(), Password.Text.Trim(),Email.Text.Trim());
            if (mbUser != null)
            {
                BindUsers();
            }
        }
    }
}
